package com.example.vueblog.common;

import com.example.vueblog.common.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一返回对象
 *
 * @author yushunshun
 * @date 2022/5/28 17:26
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -3635708990052544378L;

    private int code;
    private String message;
    private T data;

    public static <T> Result<T> ok() {
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> ok(String message, T data) {
        return new Result<>(ResultEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> ok(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> error(T data) {
        return new Result<>(ResultEnum.FAILURE.getCode(), ResultEnum.FAILURE.getMessage(), data);
    }

    public static <T> Result<T> error(String message, T data) {
        return new Result<>(ResultEnum.FAILURE.getCode(), message, data);
    }

    public static <T> Result<T> error(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> error(int code, String message) {
        return new Result<>(code, message, null);
    }

}
